package v1

import (
	"gin-wave-im/global"
	"gin-wave-im/model/request"
	"gin-wave-im/model/response"
	"gin-wave-im/service"
	"gin-wave-im/util"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

// @Tags Community
// @Summary 创建群聊
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.CreateCommunity true "名称，图标，简介"
// @Success 200 {string} string "{"code":200,"data":null,"msg":"创建成功！"}"
// @Router /v1/community/CreateCommunity [post]
func CreateCommunity(ctx *gin.Context) {
	community := &request.CreateCommunity{}
	if err := ctx.ShouldBindJSON(community);err != nil {
		response.Fail(ctx, -1, util.Error(err))
		return
	}
	userId := service.GetUserID(ctx)
	err,dstId := service.CreateCommunity(*community, userId)
	if err != nil {
		global.GnLog.Error(err.Error(), zap.Any("err", err))
		response.Fail(ctx, -1, err.Error())
	} else {
		err := service.JoinCommunity(userId, dstId)
		if err != nil {
			response.Fail(ctx, -1, err.Error())
		} else {
			response.Success(ctx, nil, "创建成功！")
		}
	}
}

// @Tags Community
// @Summary 加入群聊
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param data body request.JoinCommunity true "群聊id"
// @Success 200 {string} string "{"code":200,"data":null,"msg":"添加好友成功！"}"
// @Router /v1/community/JoinCommunity [post]
func JoinCommunity(ctx *gin.Context) {
	dst := &request.JoinCommunity{}
	if err := ctx.ShouldBindJSON(dst);err != nil {
		response.Fail(ctx, -1, util.Error(err))
		return
	}
	userId := service.GetUserID(ctx)
	err := service.JoinCommunity(userId, dst.DstId)
	if err != nil {
		response.Fail(ctx, -1, err.Error())
	} else {
		AddGroupId(userId, dst.DstId)
		response.Success(ctx, nil, "加入群聊成功！")
	}
}

// 加入群聊
func AddGroupId(userId uint64, dstId uint64) {
	rwLocker.RLock()
	node, ok := clientMap[userId]
	if ok {
		node.GroupSets.Add(dstId)
	}
	rwLocker.RUnlock()
}

// @Tags Community
// @Summary 获得用户已加入群聊的列表
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Success 200 {string} string "{"code":200,"data":[],"msg":"获取群聊列表成功！"}"
// @Router /v1/community/list [get]
func CommunityList(ctx *gin.Context) {
	userId := service.GetUserID(ctx)
	dstIds,err := service.CommunityIds(userId)
	if err != nil {
		response.Fail(ctx, -1, err.Error())
		return
	}
	communities,_ := service.CommunityList(dstIds)

	response.Success(ctx, communities, "获取群聊列表成功！")
}

// @Tags Community
// @Summary 获得群聊的成员列表
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param DstId query int true "群聊id" length(20)
// @Success 200 {string} string "{"code":200,"data":[],"msg":"获取群聊成员列表成功！"}"
// @Router /v1/community/members [get]
func MemberList(ctx *gin.Context) {
	dst := &request.JoinCommunity{}
	if err := ctx.ShouldBindQuery(dst);err != nil {
		response.Fail(ctx, -1, util.Error(err))
		return
	}
	userIds,err := service.MemberList(dst.DstId)
	if err != nil {
		response.Fail(ctx, -1, err.Error())
		return
	}
	users,err := service.UserList(userIds)
	if err != nil {
		response.Fail(ctx, -1, err.Error())
		return
	}

	response.Success(ctx, users, "获取群聊成员列表成功！")
}

// @Tags Community
// @Summary 获得群聊的信息
// @Produce application/json
// @Param Authorization header string true "验证参数Bearer和token空格拼接"
// @Param DstId query int true "群聊id" length(20)
// @Success 200 {string} string "{"code":200,"data":[],"msg":"获取群聊信息成功！"}"
// @Router /v1/community [get]
func CommunityInfo(ctx *gin.Context) {
	dst := &request.JoinCommunity{}
	if err := ctx.ShouldBindQuery(dst);err != nil {
		response.Fail(ctx, -1, util.Error(err))
		return
	}
	info,err := service.CommunityInfo(dst.DstId)
	if err != nil {
		response.Fail(ctx, -1, err.Error())
		return
	}

	response.Success(ctx, info, "获取群聊信息成功！")
}